package buggy;

import java.io.*;
import java.util.*;

// result: 
// num1 = a, num2 = b
// f(x)=(a+b)(a-b)

public class Main {
	public static void main(String[] args) throws java.io.IOException {

		int num1 = readInteger();
		System.out.println("\nYou entered the integer: " + num1);

		int num2 = reverseNumber(num1);
		System.out.println("\nreversed integer: " + num2);
		int result = result(num1, num2);

		System.out.println("The computed result is: " + result);
	}

	private static int result(int num1, int num2) {
		C1 o1 = new C1(num1, num2); // num1 + num2
		C2 o2 = new C2(num1, num2); // num1 - num2

		int result1 = o1.compute();
		int result2 = o2.compute();

		return result1 * result2; // result=(num1 + num2)(num1 - num2)
	}

	private static int reverseNumber(int number) {
		int reversedNumber = 0;
		int temp = 0;

		//If the number is negative, goes to if case
		//If the number is positive, goes to else case
		if (number < 0) {
			number = negativeNumber(number);
			number = reverseNumber(number);
			reversedNumber = -number;
		} else
			while (number > 0) {

				temp = number % 10;

				reversedNumber = reversedNumber * 10 + temp;
				number = number / 10;

			}

		return reversedNumber;
	}

	private static int negativeNumber(int number) {
		int negativeNumber = 0;
		negativeNumber = -number;
		return negativeNumber;
	}

	private static int readInteger() throws java.io.IOException {

		String s1;
		String s2;
		int num = 0;

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		boolean cont = true;

		while (cont) {

			System.out.print("Enter an integer:");
			s1 = br.readLine();
			StringTokenizer st = new StringTokenizer(s1);
			s2 = "";

			while (cont && st.hasMoreTokens()) {
				s2 = st.nextToken();
				num = Integer.parseInt(s2);
				cont = false;
			}
		}
		return num;
	}
}
